﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ReNameTools
{
    public partial class Rename : Form
    {
       private unvell.ReoGrid.Worksheet worksheet =null;
        public Rename()
        {
            InitializeComponent();
        }

        private void txtDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.txtDir.Text= dialog.SelectedPath;
                DirectoryInfo dir = new DirectoryInfo(dialog.SelectedPath);
                if (dir.Exists == false)
                {
                    MessageBox.Show("路径不存在！请重新输入");
                }
                else
                {
                    var rowCount = worksheet.RowCount;
                    worksheet["A1:A" + rowCount.ToString()] = new object[rowCount];
                    //检索表示当前目录的文件和子目录
                    FileSystemInfo[] fsinfos = dir.GetFiles();
                    //遍历检索的文件和子目录
                    for (var i = 0; i < fsinfos.Length; i++) {
                        var fsinfo = fsinfos[i];
                        var cellIndex = "A" + (i + 1);
                        var cellEIndex = "B" + (i + 1);
                        worksheet.Cells[cellIndex].Data = fsinfo.Name;
                        worksheet.Cells[cellIndex].Style.TextColor = Color.Black;

                        worksheet.Cells[cellEIndex].Style.FontSize = 14;
                        worksheet.Cells[cellEIndex].Style.Bold = true;
                    }
                }
            }
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            try
            {
                var count = worksheet.RowCount;
                var cellIndex = "A";
                var cellEIndex = "B";

                for (var i = 0; i < count; i++)
                {
                    var A = cellIndex + (i + 1);
                    var B = cellEIndex + (i + 1);
                    var oldName = worksheet.Cells[A].Data == null ? "" : worksheet.Cells[A].Data.ToString().Trim();
                    var newName = worksheet.Cells[B].Data == null ? "" : worksheet.Cells[B].Data.ToString().Trim();
                    if (!string.IsNullOrWhiteSpace(oldName) && !string.IsNullOrWhiteSpace(newName))
                    {
                        string a = this.txtDir.Text + "\\" + oldName;
                        string b = this.txtDir.Text + "\\" + newName;
                        if (File.Exists(a))
                        {
                            File.Move(a, b);
                        }
                    }
                    Debug.WriteLine(oldName + "   ==>  " + newName);
                }
                MessageBox.Show("修改文件名成功");
            }
            catch (Exception ex) {
                MessageBox.Show("修改文件名失败："+ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            worksheet = reoGrid.CurrentWorksheet;
            worksheet.SetCols(2);
            worksheet.SetColumnsWidth(1, worksheet.ColumnCount, 300);
            worksheet.SetColumnsWidth(0, worksheet.ColumnCount, 300);

        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(this.txtDir.Text);
            if (dir.Exists == false)
            {
                MessageBox.Show("路径不存在！请重新输入");
            }
            else
            {
                var rowCount = worksheet.RowCount;
                worksheet["A1:A" + rowCount.ToString()] = new object[rowCount];
                //检索表示当前目录的文件和子目录
                FileSystemInfo[] fsinfos = dir.GetFiles();
                //遍历检索的文件和子目录
                for (var i = 0; i < fsinfos.Length; i++)
                {
                    var fsinfo = fsinfos[i];
                    var cellIndex = "A" + (i + 1);
                    var cellEIndex = "B" + (i + 1);
                    worksheet.Cells[cellIndex].Data = fsinfo.Name;
                    worksheet.Cells[cellIndex].Style.TextColor = Color.Black;

                    worksheet.Cells[cellEIndex].Style.FontSize = 14;
                    worksheet.Cells[cellEIndex].Style.Bold = true;
                }
            }
        }
    }
}
